package com.doubletick.proxy.ipidea.service.impl;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.doubletick.common.annotation.DataScope;
import com.doubletick.common.utils.DateUtils;
import com.doubletick.proxy.ipidea.cache.ProxyAccountCache;
import com.doubletick.proxy.ipidea.cache.UsernameCache;
import com.doubletick.proxy.ipidea.domain.ProxyAccountFlow;
import com.doubletick.proxy.ipidea.mapper.ProxyAccountFlowMapper;
import com.doubletick.proxy.ipidea.service.IProxyAccountFlowService;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 代理账号流量Service业务层处理
 *
 * @author ruoyi
 * @date 2024-07-08
 */
@Service
public class ProxyAccountFlowServiceImpl implements IProxyAccountFlowService {
    @Autowired
    private ProxyAccountFlowMapper proxyAccountFlowMapper;

    @Resource
    private ProxyAccountCache proxyAccountCache;
    @Resource
    private UsernameCache usernameCache;

    /**
     * 查询代理账号流量
     *
     * @param id 代理账号流量主键
     * @return 代理账号流量
     */
    @Override
    public ProxyAccountFlow selectProxyAccountFlowById(Long id) {
        return proxyAccountFlowMapper.selectProxyAccountFlowById(id);
    }

    /**
     * 查询代理账号流量列表
     *
     * @param proxyAccountFlow 代理账号流量
     * @return 代理账号流量
     */
    @Override
    @DataScope(deptAlias = "u", userAlias = "u")
    public List<ProxyAccountFlow> selectProxyAccountFlowList(ProxyAccountFlow proxyAccountFlow) {
        List<ProxyAccountFlow> list = proxyAccountFlowMapper.selectProxyAccountFlowList(proxyAccountFlow);
        setProxyAccount(list);
        return list;
    }

    @Override
    public JSONArray selectProxyAccountFlowListNoPer(ProxyAccountFlow proxyAccountFlow) {
        List<ProxyAccountFlow> list = proxyAccountFlowMapper.selectProxyAccountFlowList(proxyAccountFlow);
        setProxyAccount(list);
        JSONArray array = new JSONArray();
        list.forEach(e -> {
            JSONObject json = new JSONObject();
            json.put("username", e.getProxyAccount());
            json.put("flow", e.getGlobal());
            json.put("time", DateUtils.parseDateToStr("yyyy-MM-dd HH", e.getCreateTime()));
            array.add(json);
        });
        return array;
    }


    @Override
    public JSONArray countByProxyName(String date) {
        ProxyAccountFlow proxyAccountFlow = new ProxyAccountFlow();
        proxyAccountFlow.getParams().put("beginDataTime", date);
        proxyAccountFlow.getParams().put("endDataTime", date);
        List<ProxyAccountFlow> list = proxyAccountFlowMapper.selectProxyAccountFlowListByDay(proxyAccountFlow);
        setProxyAccount(list);
        JSONArray jsonArray = new JSONArray();
        list.forEach(e -> {
            JSONObject json = new JSONObject();
            json.put("username", e.getProxyAccount());
            json.put("flow", e.getGlobal());
            json.put("time", date);
            jsonArray.add(json);
        });
        return jsonArray;
    }


    @Override
    @DataScope(deptAlias = "u", userAlias = "u")
    public List<ProxyAccountFlow> selectProxyAccountFlowListByDay(ProxyAccountFlow proxyAccountFlow) {
        List<ProxyAccountFlow> list = proxyAccountFlowMapper.selectProxyAccountFlowListByDay(proxyAccountFlow);
        setProxyAccount(list);
        return list;
    }

    @Override
    @DataScope(deptAlias = "u", userAlias = "u")
    public Object summary(ProxyAccountFlow proxyAccountFlow) {
        return proxyAccountFlowMapper.summary(proxyAccountFlow);
    }

    private void setProxyAccount(List<ProxyAccountFlow> list) {
        list.forEach(e -> {
            e.setProxyAccount(proxyAccountCache.getProxyAccountByProxyId(e.getProxyId()));
            e.setUsername(usernameCache.getUsernameByUserId(e.getUserId()));
        });
    }


    /**
     * 新增代理账号流量
     *
     * @param proxyAccountFlow 代理账号流量
     * @return 结果
     */
    @Override
    public int insertProxyAccountFlow(ProxyAccountFlow proxyAccountFlow) {
        proxyAccountFlow.setCreateTime(DateUtils.getNowDate());
        return proxyAccountFlowMapper.insertProxyAccountFlow(proxyAccountFlow);
    }

    /**
     * 修改代理账号流量
     *
     * @param proxyAccountFlow 代理账号流量
     * @return 结果
     */
    @Override
    public int updateProxyAccountFlow(ProxyAccountFlow proxyAccountFlow) {
        proxyAccountFlow.setUpdateTime(DateUtils.getNowDate());
        return proxyAccountFlowMapper.updateProxyAccountFlow(proxyAccountFlow);
    }

    /**
     * 批量删除代理账号流量
     *
     * @param ids 需要删除的代理账号流量主键
     * @return 结果
     */
    @Override
    public int deleteProxyAccountFlowByIds(Long[] ids) {
        return proxyAccountFlowMapper.deleteProxyAccountFlowByIds(ids);
    }

    /**
     * 删除代理账号流量信息
     *
     * @param id 代理账号流量主键
     * @return 结果
     */
    @Override
    public int deleteProxyAccountFlowById(Long id) {
        return proxyAccountFlowMapper.deleteProxyAccountFlowById(id);
    }

    @Override
    public void saveOrUpdate(ProxyAccountFlow accountFlow) {
        proxyAccountFlowMapper.saveOrUpdate(accountFlow);
    }
}
